﻿
var dataTB = [];
var tbEditId = -1;


$(document).ready(function () {
    initTB();

    $('a#transport-b-modal-caller').fancybox({
        showCloseButton: false,
        fitToView: true,
        autoSize: true,
        closeClick: true,
        autoDimensions: false,
        width: 640,
        openEffect: 'none',
        padding: 0,
        onStart: function () {
            if (tbEditId == -1)
                $("#transport-b-modal h1.header").text("Add Tranportation B");
            else
                $("#transport-b-modal h1.header").text("Edit Tranportation B");
        },
        onClosed: function () {
            // Reset
            tbEditId = -1;
            $("span#vldDistance").hide();
            $("span#vldTBKgLoad").hide();
        }
    });

    $("a#lnkTransportB").click(function () {
        resetTBModal();
        $('a#transport-b-modal-caller').click();

    });
});

function initTB() {
    var str = $("#txtTransportB").val();
    if (str != '') {
        dataTB = JSON.parse(str);
        for (var i = 0; i < dataTB.length; i++) {
            var item = dataTB[i];
            item.id = i;
        }
    }


    loadTBGrid();
}

function callbackFromTBModal(row) {
    processTBData(row);
}

function processTBData(row) {
    if (tbEditId != -1) {

        for (var i = 0; i < dataTB.length; i++) {
            var item = dataTB[i];
            if (item.id == tbEditId) {
                item.vehicle_id = row.vehicle_id,
                    item.vehicle_name = row.vehicle_name,
                    item.distance = row.distance,
                    item.unit_distance_id = row.unit_distance_id,
                    item.unit_distance_name = row.unit_distance_name,
					item.kg_load = row.kg_load,
					item.unit_kgload_id = row.unit_kgload_id,
					item.unit_kgload_name = row.unit_kgload_name
                break;
            }
        }



    }
    else {
        var inputRow = {
            id: getNextId(dataTB),
            vehicle_id: row.vehicle_id,
            vehicle_name: row.vehicle_name,
            distance: row.distance,
            unit_distance_id: row.unit_distance_id,
            unit_distance_name: row.unit_distance_name,
            kg_load: row.kg_load,
            unit_kgload_id: row.unit_kgload_id,
            unit_kgload_name: row.unit_kgload_name
        };

        dataTB.push(inputRow);

    }


    updateTB();
    loadTBGrid();
    focusTBGrid();
}
function deleteTBRow(id) {
    for (var i = 0; i < dataTB.length; i++) {
        var item = dataTB[i];
        if (item.id == id) {
            dataTB.splice(i, 1);
            break;
        }
    }

    updateTB();
    loadTBGrid();
    focusTBGrid();
}

function editTBRow(id) {
    var selectedRow = null;
    for (var i = 0; i < dataTB.length; i++) {
        var item = dataTB[i];
        if (item.id == id) {
            selectedRow = item;
            break;
        }
    }
    tbEditId = id;

    resetTBModal();
    loadTBModal(selectedRow);
    $('a#transport-b-modal-caller').click();
}

function updateTB() {
    $("#txtTransportB").val(JSON.stringify(dataTB));

}

function focusTBGrid() {
    $(window).scrollTop($('table#gridTransportB tr:last').offset().top);
}

function loadTBGrid() {
    $('table#gridTransportB').empty();

    var oTable = $('table#gridTransportB').dataTable({
        "bProcessing": true,
        "bDestroy": true,
        "bPaginate": false,
        "bAutoWidth": false,
        "bFilter": false,
        "aaSorting": [],
        "aaData": dataTB,
        "aoColumns": [

                    { "mDataProp": "vehicle_name", "bSortable": true, "sTitle": "Name", "sClass": "cName", "bSearchable": true, "bVisible": true },
                    { "mDataProp": "distance", "bSortable": false, "sTitle": "Distance", "sClass": "cName", "bSearchable": true, "bVisible": true },
                    { "mDataProp": "unit_distance_name", "bSortable": false, "sTitle": "Unit", "sClass": "cUnit", "bSearchable": true, "bVisible": true },
                    { "mDataProp": "kg_load", "bSortable": false, "sTitle": "Kg Load", "sClass": "cName", "bSearchable": true, "bVisible": true },
                    { "mDataProp": "unit_kgload_name", "bSortable": false, "sTitle": "Unit", "sClass": "cUnit", "bSearchable": true, "bVisible": true },
                    { "mDataProp": null, "bSortable": false, "sTitle": "", "sClass": "cDelete", "sDefaultContent": "<input id='btnEdit' class='button' type='button' value='Edit' /><input id='btnDelete' class='button' type='button' value='Delete' />", "bVisible": true }
                ],
        "fnCreatedRow": function (nRow, aData, iDataIndex) {
            // Assign id to 'tr'
            $(nRow).attr('id', aData.id);

            $('input#btnEdit', nRow).click(function (e) {
                editTBRow(aData.id);
            });

            $('input#btnDelete', nRow).click(function (e) {

                if (confirm('Are you sure to delete this?')) {
                    deleteTBRow(aData.id);
                }
            });
        }


    });
}